package com.numberone.master.modules.api.controller;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.numberone.master.modules.api.service.ApiWorkOrderService;
import com.numberone.master.modules.oa.service.WorkOrderService;

@RestController
@RequestMapping(value = "/api/workOrder/**", produces = MediaType.APPLICATION_JSON_VALUE)
public class ApiWorkOrderController {
	
	@Autowired
	private ApiWorkOrderService apiWorkOrderService;
	
	@Autowired
	private WorkOrderService workOrderService;
	
	public static Log log = LogFactory.getLog(ApiWorkOrderController.class);
	
	
	//派单和重派
	@RequestMapping(value = "/distribute",method = RequestMethod.POST)
	public String distribute(@RequestBody  String str) {
		log.info(str);
		JSONObject rtn = new JSONObject();
		try {
			if(!StringUtils.isEmpty(str)) {
				apiWorkOrderService.insertApiWorkOrder(str);
				//判断重派
				if(apiWorkOrderService.repetition(str)){
					apiWorkOrderService.reDistribute(str);
					rtn.put("rtnCode", "0");
					return rtn.toString();
				}
				apiWorkOrderService.insertWorkOrder(str);
				rtn.put("rtnCode", "0");
				return rtn.toString();	
			} else {
				rtn.put("rtnCode", "-9999");
				rtn.put("returnMessage", "");
				return rtn.toString();
			}
		} catch (Exception e) {
			log.error(e);
			rtn.put("rtnCode", "-9999");
			rtn.put("returnMessage", e.toString());
			return rtn.toString();
		}
		
	}
	
	//归档
	@RequestMapping(value = "/archive",method = RequestMethod.POST)
	public String archive(@RequestBody  String str) {
		log.info(str);
		JSONObject rtn = new JSONObject();
		try {
			if(!StringUtils.isEmpty(str)) {
				apiWorkOrderService.insertApiWorkOrder(str);
				apiWorkOrderService.archive(str);
				rtn.put("rtnCode", "0");
				return rtn.toString();	
			} else {
				rtn.put("rtnCode", "-9999");
				return rtn.toString();
			}
		} catch (Exception e) {
			log.error(e);
			rtn.put("rtnCode", "-9999");
			rtn.put("returnMessage", e.toString());
			return rtn.toString();
		}
	}
	
	//撤单
	@RequestMapping(value = "/revoke",method = RequestMethod.POST)
	public String revoke(@RequestBody String str){
		log.info(str);
		JSONObject rtn = new JSONObject();
		//确认撤单
		try {
			if(!StringUtils.isEmpty(str)) {
				apiWorkOrderService.insertApiWorkOrder(str);
				apiWorkOrderService.revoke(str);
				rtn.put("rtnCode", "0");
				return rtn.toString();
			} else {
				rtn.put("rtnCode", "-9999");
				rtn.put("returnMessage", "确认撤单失败！");
				return rtn.toString();
			}
		} catch (Exception e) {
			log.error(e);
			rtn.put("rtnCode", "-9999");
			rtn.put("returnMessage", e.toString());
			return rtn.toString();
		}
	}
	
	//催单
	@RequestMapping(value = "/urge",method = RequestMethod.POST) 
	public String urge(@RequestBody String str){
		log.info(str);
		JSONObject rtn = new JSONObject();
		try {
			if(!StringUtils.isEmpty(str)) {
				apiWorkOrderService.insertApiWorkOrder(str);
				apiWorkOrderService.urge(str);
				rtn.put("rtnCode", "0");
				return rtn.toString();	
			} else {
				rtn.put("rtnCode", "-9999");
				return rtn.toString();
			}
		} catch (Exception e) {
			log.error(e);
			rtn.put("rtnCode", "-9999");
			rtn.put("returnMessage", e.toString());
			return rtn.toString();
		}
		
	}
	
	//中途意见
	@RequestMapping(value = "/suggestion",method = RequestMethod.POST)
	public String suggestion(@RequestBody String str){
		log.info(str);
		JSONObject rtn = new JSONObject();
		try {
			if(!StringUtils.isEmpty(str)) {
				apiWorkOrderService.insertApiWorkOrder(str);
				apiWorkOrderService.suggestion(str);
				rtn.put("rtnCode", "0");
				return rtn.toString();	
			} else {
				rtn.put("rtnCode", "-9999");
				return rtn.toString();
			}
		} catch (Exception e) {
			log.error(e);
			rtn.put("rtnCode", "-9999");
			rtn.put("returnMessage", e.toString());
			return rtn.toString();
		}
		
	}
	
	//退回
	@RequestMapping(value = "/retreat",method = RequestMethod.POST)
	public String retreat(@RequestBody String str){
		log.info(str);
		JSONObject rtn = new JSONObject();
		try {
			if(!StringUtils.isEmpty(str)) {
				apiWorkOrderService.insertApiWorkOrder(str);
				apiWorkOrderService.retreat(str);
				rtn.put("rtnCode", "0");
				return rtn.toString();
			} else {
				rtn.put("rtnCode", "-9999");
				return rtn.toString();
			}
		} catch (Exception e) {
			log.error(e);
			rtn.put("rtnCode", "-9999");
			rtn.put("returnMessage", e.toString());
			return rtn.toString();
		}
		
	}
	
	//进度查询
	@RequestMapping(value = "/getSchedule",method = RequestMethod.POST)
	public String getSchedule(@RequestBody String str){
		log.info(str);
		JSONObject rtn = new JSONObject();
		try {
			if(!StringUtils.isEmpty(str)) {
				apiWorkOrderService.insertApiWorkOrder(str);
				rtn = apiWorkOrderService.getSchedule(str);
				return rtn.toString();
			} else {
				rtn.put("rtnCode", "-9999");
				return rtn.toString();
			}
		} catch (Exception e) {
			log.error(e);
			rtn.put("rtnCode", "-9999");
			rtn.put("returnMessage", e.toString());
			return rtn.toString();
		}
		
	}
	
	@RequestMapping(value = "/test",method = RequestMethod.POST)
	public String test(@RequestBody  String str) {
		log.info(str);
		JSONObject rtn = new JSONObject();
		JSONArray beans = new JSONArray();
		for(int i=0;i<5;i++){
			JSONObject bean = new JSONObject();
			bean.put("processDealStaff", "中文测试"+i);
			bean.put("staffContactPhone", "啦啦"+i);
			bean.put("processDealOrgaName", "阿萨德"+i);
			bean.put("processDealTime", "阿达深层次"+i);
			bean.put("processDealComment", "阿斯蒂芬"+i);
			beans.add(bean);
		}
		rtn.put("rtnCode", "0");
		rtn.put("beans", beans);
		System.out.println("收到的数据 ："+str);
		return rtn.toString();
	}
	
}
